package com.sunseagear.wind.modules.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.base.tenant.TenantProperties;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.monitor.entity.OperationLog;
import com.sunseagear.wind.modules.monitor.entity.query.OperationLogQuery;
import com.sunseagear.wind.modules.monitor.service.IOperationLogService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sys.controller
 * @title: 操作日志控制器
 * @description: 操作日志控制器 * @date: 2018-09-30 15:53:25
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("monitor/operation/log")
@Log(title = "操作日志")
@RequiredArgsConstructor
public class OperationLogController extends BaseBeanController {

    private final IOperationLogService operationLogService;

    @PostConstruct
    protected void init() {
        TenantProperties.getInstance().getIgnoreTables().add("sys_operation_log");
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("monitor:operation:log:list")
    public RPage<OperationLog> list(OperationLogQuery query) {
        //加入条件
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(OperationLog::getCreateDate)
                .eq(ObjectUtils.isNotEmpty(query.getOperationIp()),OperationLog::getOperationIp, query.getOperationIp())
                .like(ObjectUtils.isNotEmpty(query.getRequestUri()),OperationLog::getRequestUri, query.getRequestUri());
                // 预处理
        Page<OperationLog> pageBean = operationLogService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 查询需求
     * @param id 主键ID
     * @return 返回结果
     */
    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("monitor:operation:log:detail")
    public R<OperationLog> detail(@PathVariable("id") Long id) {
        OperationLog operationLog = operationLogService.getById(id);
        return R.success(operationLog);
    }

    /**
     * 批量删除
     * @param idList 主键ID列表
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("monitor:operation:log:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        operationLogService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
